package com.enjoy.rocketmq.simple;

import com.enjoy.rocketmq.Util;
import org.apache.rocketmq.client.log.ClientLogger;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 简单样例：同步发送消息
 */
public class Producer {
    public static void main(String[] args) throws Exception {

        Util.loadProperties();
        // 在ClientLogger中定义了一些常量来配置系统的环境变量，启用slf4j，默认启用inner
        System.setProperty(ClientLogger.CLIENT_LOG_USESLF4J, "true");

        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
        producer.setNamesrvAddr(System.getProperty("nameserver.addr"));

        producer.start();
        for (int i = 0; i < 20; i++) {
            Message msg = new Message("TopicTest", "TagA", "OrderID188", "hello,world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            // 同步传递消息，消息会发送给集群中的一个Broker节点
            SendResult sendResult = producer.send(msg);
            // 这个方法是单向发送消息
            // 没有返回值，也没有回调，就是只管把消息发出去就行了
//            producer.sendOneway(msg);
            System.out.printf("%s%n", sendResult);
        }
        producer.shutdown();
        System.out.println("发送完毕");

    }
}
